package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.Result;
import com.example.demo.entity.Menu;
import com.example.demo.dao.MenuMapper;
import com.example.demo.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wms
 * @since 2022-10-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    MenuService menuService;

    @Override
    public List<Menu> menulist(String roleId) {
        List<Menu> list = menuService.lambdaQuery().like(Menu::getMenuright,roleId).list();
        List<Menu> menuTree = builderMenuTree(list);
        return menuTree;
    }

    private List<Menu> builderMenuTree(List<Menu> list) {
        List<Menu> menuTree = list.stream()
                .filter(menu -> menu.getMenulevel().equals("1"))
                .map(menu -> menu.setChildren(getChildren(menu, list)))
                .collect(Collectors.toList());
        return  menuTree;
    }

    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getMenuparentcode().equals(menu.getMenucode()))
                .filter(m -> m.getMenulevel().equals("2"))
                .collect(Collectors.toList());
        return childrenList;
    }



}
